package com.solexnlp.simplesemantic;

import android.content.Context;
import android.util.Log;

import com.solexnlp.simplesemantic.api.APIDefine;
import com.solexnlp.simplesemantic.core.SimpleSemanticCore;
import com.solexnlp.simplesemantic.data.AnalysisResult;
import com.solexnlp.simplesemantic.data.SimpleSemanticConfiguration;
import com.solexnlp.simplesemantic.settings.SDKConfig;
import com.solexnlp.simplesemantic.settings.TypeDefine;
import com.solexnlp.simplesemantic.utils.SDKUtils;

public class SimpleSemantic extends TypeDefine implements APIDefine {

    private final String TAG = "SimpleSemantic";

    private static SimpleSemantic instance;

    private SimpleSemanticCore mCore;

    private SimpleSemantic() {}

    /**
     * 获取单例对象的静态方法
     *
     * @return 单例对象
     */
    public static synchronized SimpleSemantic getPredictor() {
        if (instance == null) {
            instance = new SimpleSemantic();
        }

        return instance;
    }

    /**
     * 全局初始化, 懒汉单例，仅需执行一次
     * note: 初始化采用异步初始化
     *
     * @param ctx           输入Context
     * @param configuration 输入配置参数
     */
    @Override
    public void init(Context ctx, SimpleSemanticConfiguration configuration) {
        if (mCore == null) {
            String resourceFolderPath = ctx.getExternalFilesDir(null).getAbsolutePath() + "/" + SDKConfig.packDirName + "/";
            SDKUtils.copyFilesFromAssets(ctx, SDKConfig.packDirName, resourceFolderPath);
            Log.i(TAG, "resource: " + resourceFolderPath);
            if (configuration.getFolderPath() == null || "".equals(configuration.getFolderPath())) {
                configuration.setFolderPath(resourceFolderPath);
            }

            mCore = new SimpleSemanticCore();
            mCore.init(configuration);
        }
    }

    /**
     * 分析句子
     *
     * @param sentence 输入字符串
     * @return 返回分析结果
     */
    @Override
    public AnalysisResult analysis(String sentence) {
        if (mCore != null) {
            return mCore.analysisOfSentence(sentence);
        }
        AnalysisResult result = new AnalysisResult();
        result.setExecCode(-1);
        Log.e(TAG, "Initialization is not performed.");

        return result;
    }


}
